home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / mig / dist / header.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-29  |  8.7 KB  |  311 lines

  1. /* 
  2.  * Mach Operating System
  3.  * Copyright (c) 1991,1990 Carnegie Mellon University
  4.  * All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify and distribute this software and its
  7.  * documentation is hereby granted, provided that both the copyright
  8.  * notice and this permission notice appear in all copies of the
  9.  * software, derivative works or modified versions, and any portions
  10.  * thereof, and that both notices appear in supporting documentation.
  11.  * 
  12.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 
  13.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15.  * 
  16.  * Carnegie Mellon requests users of this software to return to
  17.  * 
  18.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  19.  *  School of Computer Science
  20.  *  Carnegie Mellon University
  21.  *  Pittsburgh PA 15213-3890
  22.  * 
  23.  * any improvements or extensions that they make and grant Carnegie the
  24.  * rights to redistribute these changes.
  25.  */
  26. /*
  27.  * HISTORY
  28.  * $Log:    header.c,v $
  29.  * Revision 2.7  91/08/28  11:16:58  jsb
  30.  *     Removed TrapRoutine support.
  31.  *     [91/08/12            rpd]
  32.  * 
  33.  * Revision 2.6  91/06/26  14:39:37  rpd
  34.  *     Removed the user initialization function and InitRoutineName.
  35.  *     Fixed to use different symbolic constants to protect
  36.  *     the user and server header files against multiple inclusion.
  37.  *     [91/06/26            rpd]
  38.  * 
  39.  * Revision 2.5  91/06/25  10:31:21  rpd
  40.  *     Restored prototype generation.
  41.  *     Changed WriteHeader to WriteUserHeader.
  42.  *     Added WriteServerHeader.
  43.  *     [91/05/23            rpd]
  44.  * 
  45.  * Revision 2.4  91/02/05  17:54:33  mrt
  46.  *     Changed to new Mach copyright
  47.  *     [91/02/01  17:54:19  mrt]
  48.  * 
  49.  * Revision 2.3  90/12/20  17:04:47  jeffreyh
  50.  *     Commented out code for prototype generation. This is a temporary solution
  51.  *     to the longer term problem of the need 
  52.  *     for the generation of both a client and a server header file 
  53.  *     that have correct prototypes for strict ansi c and c++. The
  54.  *     prototypes generated before anly were for the client and broke kernel
  55.  *     files that included them if compiled under standard gcc
  56.  *     [90/12/07            jeffreyh]
  57.  * 
  58.  * Revision 2.2  90/06/02  15:04:46  rpd
  59.  *     Created for new IPC.
  60.  *     [90/03/26  21:11:06  rpd]
  61.  * 
  62.  * 07-Apr-89  Richard Draves (rpd) at Carnegie-Mellon University
  63.  *    Extensive revamping.  Added polymorphic arguments.
  64.  *    Allow multiple variable-sized inline arguments in messages.
  65.  *
  66.  *  8-Jul-88  Mary Thompson (mrt) at Carnegie-Mellon University
  67.  *    Conditionally defined mig_external to be extern and then defined
  68.  *    all functions  with the storage class mig_external.
  69.  *    Mig_external can be changed
  70.  *    when the generated code is compiled.
  71.  *
  72.  * 18-Jan-88  David Detlefs (dld) at Carnegie-Mellon University
  73.  *    Modified to produce C++ compatible code via #ifdefs.
  74.  *    All changes have to do with argument declarations.
  75.  *
  76.  *  3-Aug-87  Mary Thompson (mrt) at Carnegie-Mellon University
  77.  *    Revision to make multi-threaded use work. Removed definitions for
  78.  *     alloc_reply_port and init_msg_type as these routines are 
  79.  *     no longer generated.
  80.  *
  81.  * 30-Jul-87  Mary Thompson (mrt) at Carnegie-Mellon University
  82.  *     Made changes to generate conditional code for C++ parameter lists
  83.  *
  84.  * 29-Jul-87  Mary Thompson (mrt) at Carnegie-Mellon University
  85.  *    Changed WriteRoutine to produce conditional argument
  86.  *    lists for C++
  87.  *
  88.  *  8-Jun-87  Mary Thompson (mrt) at Carnegie-Mellon University
  89.  *    Changed the KERNEL include from ../h to sys/
  90.  *    Removed extern from WriteHeader to make hi-c happy
  91.  *
  92.  * 28-May-87  Richard Draves (rpd) at Carnegie-Mellon University
  93.  *    Created.
  94.  */
  95.  
  96. #include "write.h"
  97. #include "utils.h"
  98. #include "global.h"
  99.  
  100. static void
  101. WriteIncludes(file)
  102.     FILE *file;
  103. {
  104.     fprintf(file, "#include <mach/kern_return.h>\n");
  105.     fprintf(file, "#if\t%s || %s\n", NewCDecl, LintLib);
  106.     fprintf(file, "#include <mach/port.h>\n");
  107.     fprintf(file, "#include <mach/message.h>\n");
  108.     fprintf(file, "#endif\n");
  109.     fprintf(file, "\n");
  110. }
  111.  
  112. static void
  113. WriteDefines(file)
  114.     FILE *file;
  115. {
  116. }
  117.  
  118. static void
  119. WriteMigExternal(file)
  120.     FILE *file;
  121. {
  122.     fprintf(file, "#ifdef\tmig_external\n");
  123.     fprintf(file, "mig_external\n");
  124.     fprintf(file, "#else\n");
  125.     fprintf(file, "extern\n");
  126.     fprintf(file, "#endif\n");
  127. }
  128.  
  129. static void
  130. WriteProlog(file, protect)
  131.     FILE *file;
  132.     char *protect;
  133. {
  134.     if (protect != strNULL) {
  135.     fprintf(file, "#ifndef\t_%s\n", protect);
  136.     fprintf(file, "#define\t_%s\n", protect);
  137.     fprintf(file, "\n");
  138.     }
  139.  
  140.     fprintf(file, "/* Module %s */\n", SubsystemName);
  141.     fprintf(file, "\n");
  142.  
  143.     WriteIncludes(file);
  144.     WriteDefines(file);
  145. }
  146.  
  147. static void
  148. WriteEpilog(file, protect)
  149.     FILE *file;
  150.     char *protect;
  151. {
  152.     if (protect != strNULL) {
  153.     fprintf(file, "\n");
  154.     fprintf(file, "#endif\t_%s\n", protect);
  155.     }
  156. }
  157.  
  158. static void
  159. WriteUserRoutine(file, rt)
  160.     FILE *file;
  161.     routine_t *rt;
  162. {
  163.     fprintf(file, "\n");
  164.     fprintf(file, "/* %s %s */\n", rtRoutineKindToStr(rt->rtKind), rt->rtName);
  165.     WriteMigExternal(file);
  166.     fprintf(file, "%s %s\n", ReturnTypeStr(rt), rt->rtUserName);
  167.     fprintf(file, "#if\t%s\n", LintLib);
  168.     fprintf(file, "    (");
  169.     WriteList(file, rt->rtArgs, WriteNameDecl, akbUserArg, ", " , "");
  170.     fprintf(file, ")\n");
  171.     WriteList(file, rt->rtArgs, WriteUserVarDecl, akbUserArg, ";\n", ";\n");
  172.     fprintf(file, "{ ");
  173.     if (!rt->rtProcedure)
  174.     fprintf(file, "return ");
  175.     fprintf(file, "%s(", rt->rtUserName);
  176.     WriteList(file, rt->rtArgs, WriteNameDecl, akbUserArg, ", ", "");
  177.     fprintf(file, "); }\n");
  178.     fprintf(file, "#else\n");
  179.     fprintf(file, "#if\t%s\n", NewCDecl);
  180.     fprintf(file, "(\n");
  181.     WriteList(file, rt->rtArgs, WriteUserVarDecl, akbUserArg, ",\n", "\n");
  182.     fprintf(file, ");\n");
  183.     fprintf(file, "#else\n");
  184.  
  185.     fprintf(file, "    ();\n");
  186.     fprintf(file, "#endif\n"); 
  187.     fprintf(file, "#endif\n");
  188. }
  189.  
  190. void
  191. WriteUserHeader(file, stats)
  192.     FILE *file;
  193.     statement_t *stats;
  194. {
  195.     register statement_t *stat;
  196.     char *protect = strconcat(SubsystemName, "_user_");
  197.  
  198.     WriteProlog(file, protect);
  199.     for (stat = stats; stat != stNULL; stat = stat->stNext)
  200.     switch (stat->stKind)
  201.     {
  202.       case skRoutine:
  203.         WriteUserRoutine(file, stat->stRoutine);
  204.         break;
  205.       case skImport:
  206.       case skUImport:
  207.         WriteImport(file, stat->stFileName);
  208.         break;
  209.       case skSImport:
  210.         break;
  211.       default:
  212.         fatal("WriteHeader(): bad statement_kind_t (%d)",
  213.           (int) stat->stKind);
  214.     }
  215.     WriteEpilog(file, protect);
  216. }
  217.  
  218. static void
  219. WriteServerRoutine(file, rt)
  220.     FILE *file;
  221.     routine_t *rt;
  222. {
  223.     fprintf(file, "\n");
  224.     fprintf(file, "/* %s %s */\n", rtRoutineKindToStr(rt->rtKind), rt->rtName);
  225.     WriteMigExternal(file);
  226.     fprintf(file, "%s %s\n", ReturnTypeStr(rt), rt->rtServerName);
  227.     fprintf(file, "#if\t%s\n", LintLib);
  228.     fprintf(file, "    (");
  229.     WriteList(file, rt->rtArgs, WriteNameDecl, akbServerArg, ", " , "");
  230.     fprintf(file, ")\n");
  231.     WriteList(file, rt->rtArgs, WriteServerVarDecl,
  232.           akbServerArg, ";\n", ";\n");
  233.     fprintf(file, "{ ");
  234.     if (!rt->rtProcedure)
  235.     fprintf(file, "return ");
  236.     fprintf(file, "%s(", rt->rtServerName);
  237.     WriteList(file, rt->rtArgs, WriteNameDecl, akbServerArg, ", ", "");
  238.     fprintf(file, "); }\n");
  239.     fprintf(file, "#else\n");
  240.     fprintf(file, "#if\t%s\n", NewCDecl);
  241.     fprintf(file, "(\n");
  242.     WriteList(file, rt->rtArgs, WriteServerVarDecl,
  243.           akbServerArg, ",\n", "\n");
  244.     fprintf(file, ");\n");
  245.     fprintf(file, "#else\n");
  246.  
  247.     fprintf(file, "    ();\n");
  248.     fprintf(file, "#endif\n"); 
  249.     fprintf(file, "#endif\n");
  250. }
  251.  
  252. void
  253. WriteServerHeader(file, stats)
  254.     FILE *file;
  255.     statement_t *stats;
  256. {
  257.     register statement_t *stat;
  258.     char *protect = strconcat(SubsystemName, "_server_");
  259.  
  260.     WriteProlog(file, protect);
  261.     for (stat = stats; stat != stNULL; stat = stat->stNext)
  262.     switch (stat->stKind)
  263.     {
  264.       case skRoutine:
  265.         WriteServerRoutine(file, stat->stRoutine);
  266.         break;
  267.       case skImport:
  268.       case skSImport:
  269.         WriteImport(file, stat->stFileName);
  270.         break;
  271.       case skUImport:
  272.         break;
  273.       default:
  274.         fatal("WriteServerHeader(): bad statement_kind_t (%d)",
  275.           (int) stat->stKind);
  276.     }
  277.     WriteEpilog(file, protect);
  278. }
  279.  
  280. static void
  281. WriteInternalRedefine(file, rt)
  282.     FILE *file;
  283.     register routine_t *rt;
  284. {
  285.     fprintf(file, "#define %s %s_external\n",
  286.         rt->rtUserName, rt->rtUserName);
  287. }
  288.  
  289. void
  290. WriteInternalHeader(file, stats)
  291.     FILE *file;
  292.     statement_t *stats;
  293. {
  294.     register statement_t *stat;
  295.  
  296.     for (stat = stats; stat != stNULL; stat = stat->stNext)
  297.     switch (stat->stKind)
  298.     {
  299.       case skRoutine:
  300.         WriteInternalRedefine(file, stat->stRoutine);
  301.         break;
  302.       case skImport:
  303.       case skUImport:
  304.       case skSImport:
  305.         break;
  306.       default:
  307.         fatal("WriteInternalHeader(): bad statement_kind_t (%d)",
  308.           (int) stat->stKind);
  309.     }
  310. }
  311.